package com.qdxht.dispenser.ui.activity.main;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;

//import com.angcyo.rcode.RCode;
import com.qdxht.dispenser.R;
import com.qdxht.dispenser.base.BaseActivity;
import com.qdxht.dispenser.databinding.ActivityQrcodePreviewBinding;
import com.qdxht.dispenser.utils.ConfigUtil;
import com.qdxht.dispenser.utils.RoundedQRCode;

/**
 * 二维码预览页面
 */
public class QRCodePreviewActivity extends BaseActivity<ActivityQrcodePreviewBinding> {

    private static final String EXTRA_QR_SIZE = "extra_qr_size";
    private static final String EXTRA_QR_H_OFFSET = "extra_qr_h_offset";
    private static final String EXTRA_QR_V_OFFSET = "extra_qr_v_offset";

    public static void start(Context context, int qrSize, int horizontalOffset, int verticalOffset) {
        Intent intent = new Intent(context, QRCodePreviewActivity.class);
        intent.putExtra(EXTRA_QR_SIZE, qrSize);
        intent.putExtra(EXTRA_QR_H_OFFSET, horizontalOffset);
        intent.putExtra(EXTRA_QR_V_OFFSET, verticalOffset);
        context.startActivity(intent);
    }

    @Override
    protected ActivityQrcodePreviewBinding getBinding() {
        return ActivityQrcodePreviewBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 设置全屏
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);

        // 隐藏状态栏和导航栏
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        decorView.setSystemUiVisibility(uiOptions);

        // 点击任意位置退出
        binding.getRoot().setOnClickListener(v -> finish());
    }

    @Override
    protected void initView() {
        // 不需要额外初始化，布局中已设置好
    }

    @Override
    protected void initData() {
        // 获取传入的参数
        int qrSize = getIntent().getIntExtra(EXTRA_QR_SIZE, ConfigUtil.getQRCodeSize());
        int horizontalOffset = getIntent().getIntExtra(EXTRA_QR_H_OFFSET, 0);
        int verticalOffset = getIntent().getIntExtra(EXTRA_QR_V_OFFSET, 0);

        // 生成二维码
        String qrContent = ConfigUtil.getQRCode();
        if (qrContent == null || qrContent.isEmpty()) {
            qrContent = " "; // 默认内容
        }
        // 生成二维码Bitmap
        Bitmap qrBitmap = RoundedQRCode.create(this,
                qrContent, // 二维码内容
                qrSize,                   // 二维码大小
                BitmapFactory.decodeResource(getResources(), R.mipmap.ic_qr_logo), // 自定义Logo
                Color.parseColor("#24A34C")             // 二维码颜色
        );

        // 设置二维码
        binding.qrCode.setImageBitmap(qrBitmap);
        
        // 设置二维码位置
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) binding.llQrcode.getLayoutParams();
        params.leftMargin = horizontalOffset;
        params.bottomMargin = verticalOffset;
        binding.llQrcode.setLayoutParams(params);
    }
} 